//
//  NSString+TitleSize.m
//  Common
//
//  Created by Damon on 15/4/10.
//  Copyright (c) 2016年 FashonFinger. All rights reserved.
//

#import "NSString+TitleSize.h"

@implementation NSString (TitleSize)

- (CGSize)sizeWithStringFont:(UIFont *)font constrainedToSize:(CGSize)size {
    if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 7.0) {
        NSDictionary *attributes = @{NSFontAttributeName:font};
        return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
    }
    else{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        return [self sizeWithFont:font constrainedToSize:size];
#pragma clang diagnostic pop
    }
}

//带行间距的算字体高度方法
- (CGSize)sizeWithStringFont:(UIFont *)font constrainedToSize:(CGSize)size lineSpacing:(CGFloat)lineSpacing{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle1 setLineSpacing:lineSpacing];
        NSDictionary *attributes = @{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle1};
        return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    }else{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        return [self sizeWithStringFont:font constrainedToSize:size];
#pragma clang diagnostic pop
    }
}

- (CGSize)sizeWithStringFont:(UIFont *)font {
    if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] == NSOrderedDescending) {
        NSDictionary *attributes = @{NSFontAttributeName:font};
        return [self boundingRectWithSize:CGSizeZero options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    }else{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        return [self sizeWithFont:font];
#pragma clang diagnostic pop
    }
}

- (CGSize)sizeWithStringFont:(UIFont *)font constrainedToSize:(CGSize)size HadSpace:(BOOL)hasSpace{
    if (hasSpace) {
        CGFloat width = 0;
        CGFloat height = 0;
        NSArray *strings = [self componentsSeparatedByString:@"\n"];
        for (int i=0; i<strings.count; i++) {
            NSString *subString = strings[i];
            
            if ([subString isEqual:@""]) {
                height += [@"hahah" sizeWithStringFont:font constrainedToSize:size].height;
            }
            else{
                CGSize newSize = [subString sizeWithStringFont:font constrainedToSize:size];
                if (width<newSize.width) {
                    width = newSize.width;
                }
                height += newSize.height;
            }
        }
        
        height += [@"hahah" sizeWithStringFont:font constrainedToSize:size].height;
        
        return CGSizeMake(width, height);
    }
    else{
        return [self sizeWithStringFont:font constrainedToSize:size];
    }
}

@end
